package com.example.work_news;

import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.View;
import android.widget.Button;

import com.google.gson.Gson;

import java.io.IOException;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;

public class MainActivity extends AppCompatActivity {
    private Button top;
    private Button shehui;
    private Button guonei;
    private Button guoji;
    private Button yule;
    private Handler handler;
    private NewsData newsData;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        top = findViewById(R.id.b_top);
        shehui = findViewById(R.id.b_shehui);
        guonei = findViewById(R.id.b_guonei);
        guoji = findViewById(R.id.b_guoji);
        yule = findViewById(R.id.b_yule);
        initHandler();
        getDataFromApi("top");
        top.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                getDataFromApi("top");
            }
        });
        shehui.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                getDataFromApi("shehui");
            }
        });
        guonei.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                getDataFromApi("guonei");
            }
        });
        guoji.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                getDataFromApi("guoji");
            }
        });
        yule.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                getDataFromApi("yule");
            }
        });
    }

    private void initHandler() {
        handler = new Handler(new Handler.Callback() {
            @Override
            public boolean handleMessage(Message message) {
                if(message.what == 1000){
                    RecyclerView recyclerView;
                    MyAdapter adapter;
                    newsData = (NewsData)message.obj;
                    recyclerView = findViewById(R.id.list_view);
                    LinearLayoutManager layoutManager = new LinearLayoutManager(MainActivity.this);
                    recyclerView.setLayoutManager(layoutManager);
                    adapter = new MyAdapter(MainActivity.this,newsData);
                    recyclerView.setAdapter(adapter);
                }
                return true;
            }
        });
    }

    private void getDataFromApi(String type) {
         String url = "http://v.juhe.cn/toutiao/index?type="+type+"&key=6f20e93ab53e65c632173caa725cdec0";
        OkHttpClient client = new OkHttpClient();
        Request request = new Request.Builder().url(url).build();
        client.newCall(request).enqueue(new Callback() {
            @Override
            public void onFailure(Call call, IOException e) {
                Log.e("luyunshi","请求数据失败");
            }

            @Override
            public void onResponse(Call call, Response response) throws IOException {
                //response.body().string() 这个方法只能调用一次
                String newsDataJsonStr = response.body().string();
                newsData = new Gson().fromJson(newsDataJsonStr,NewsData.class);
                Log.e("luyunshi",""+  Log.e("luyunshi",""+"adapter:"+newsData.getResult().getData().get(0).getTitle()));
                Message message = Message.obtain();
                message.what = 1000;
                message.obj = newsData;
                handler.sendMessage(message);
            }
        });
    }
}
